<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '管理员添加' : '管理员修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      user:{{ user }}
      <el-form :model="user">
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="user.roleid">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input
            v-model="user.password"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqrolelist,
  requseradd,
  requseredit,
  requserinfo,
} from "../../../http/api";
import { formClose } from "../../../mixins";
import { successAlert } from "../../../utils/alert";
export default {
  // 4.接收info
  props: ["info", "arr"],
  mixins: [formClose],
  data() {
    return {
      // 5.1 初始化表单数据
      user: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
      //角色的列表
      roleList: [],
    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {
    // 请求角色的列表
    reqrolelist().then((res) => {
      if (res.data.code === 200) {
        this.roleList = res.data.list ? res.data.list : [];
      }
    });
  },
  methods: {
    ...mapActions({}),

    // 清空user
    empty() {
      this.user = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
    },
    // 5.4 点了添加
    add() {
      requseradd(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.$emit("init");
        }
      });
    },

    // 8. 获取一条数据
    getOne(id) {
      requserinfo({ uid: id }).then((res) => {
        if (res.data.code === 200) {
          this.user = res.data.list;

          //处理密码
          this.user.password=""
        }
      });
    },
    // 9.修改
    update() {
      
      requseredit(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          //如果user.uid===userInfo.uid,清空用户数据，并跳转到登录------------看这里------------------------------
          if(this.user.uid===this.userInfo.uid){
            this.changeUserInfo({})
            this.$router.push("/login")
            return;
          }
          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.$emit("init");
        }
      });
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>